﻿using System;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;
using System.Xml.Linq;
using Ice.Extension;
using IndustrialSwitchWriteMac.Core;

namespace IndustrialSwitchWriteMac
{
    public partial class ConfigForm : Form
    {
        private Config _config;
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            var names = SerialPort.GetPortNames();

            _config = IniHelper.GetConfig();
            txtTitle.Text = _config.Title;
            txtLogDir.Text = _config.LogDir;
            txtIp.Text = _config.Ip;
            txtPassword.Text = _config.Password;
            txtAccount.Text = _config.Account;

            txtMacPrefix.Text = _config.MacPrefix;

            txtLocalIp.Text = _config.LocalIp;
            cboSwitchType.Text = _config.SwitchType;
        }


        private void Button1_Click(object sender, EventArgs e)
        {
            _config.Title = txtTitle.Text;
            _config.LogDir = txtLogDir.Text;
            _config.Ip = txtIp.Text;
            _config.Account = txtAccount.Text;
            _config.Password = txtPassword.Text;
            _config.MacPrefix = txtMacPrefix.Text;

            _config.LocalIp = txtLocalIp.Text;
            _config.SwitchType = cboSwitchType.Text;

            if (string.IsNullOrEmpty(_config.Ip))
            {
                MessageBox.Show("IP地址不能为空");
                return;
            }
            //if (string.IsNullOrEmpty(_config.LocalIp))
            //{
            //    MessageBox.Show("本地网卡IP地址不能为空");
            //    return;
            //}
            if (string.IsNullOrEmpty(_config.MacPrefix) || _config.MacPrefix.Length != 6)
            {
                MessageBox.Show("MAC前6位不能为空或不是6位");
                return;
            }



            IniHelper.SaveConfig(_config);
            DialogResult = DialogResult.OK;
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtLogDir.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
